package com.titleevaluation.service;

import com.titleevaluation.entity.Application;
import com.titleevaluation.entity.CurrentQualifications;
import com.titleevaluation.repository.ApplicationRepository;
import com.titleevaluation.repository.CurrentQualificationsRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class CurrentQualificationsService {

    private final CurrentQualificationsRepository currentQualificationsRepository;
    private final ApplicationRepository applicationRepository;

    public CurrentQualificationsService(CurrentQualificationsRepository currentQualificationsRepository, ApplicationRepository applicationRepository) {
        this.currentQualificationsRepository = currentQualificationsRepository;
        this.applicationRepository = applicationRepository;
    }

    public CurrentQualifications createCurrentQualifications(Long applicationId, CurrentQualifications currentQualifications) {
        Application application = applicationRepository.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("Application not found"));
        currentQualifications.setApplication(application);
        currentQualifications.setCreatedAt(LocalDateTime.now());
        currentQualifications.setUpdatedAt(LocalDateTime.now());
        return currentQualificationsRepository.save(currentQualifications);
    }

    public List<CurrentQualifications> getCurrentQualificationsByApplicationId(Long applicationId) {
        return currentQualificationsRepository.findByApplicationId(applicationId);
    }

    public Optional<CurrentQualifications> getCurrentQualificationsById(Long id) {
        return currentQualificationsRepository.findById(id);
    }

    public CurrentQualifications updateCurrentQualifications(Long id, CurrentQualifications updatedCurrentQualifications) {
        CurrentQualifications existingCurrentQualifications = currentQualificationsRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("CurrentQualifications not found"));

        existingCurrentQualifications.setQualificationName(updatedCurrentQualifications.getQualificationName());
        existingCurrentQualifications.setQualificationLevel(updatedCurrentQualifications.getQualificationLevel());
        existingCurrentQualifications.setProfession(updatedCurrentQualifications.getProfession());
        existingCurrentQualifications.setIssuingAuthority(updatedCurrentQualifications.getIssuingAuthority());
        existingCurrentQualifications.setCertificateNo(updatedCurrentQualifications.getCertificateNo());
        existingCurrentQualifications.setIssuingUnit(updatedCurrentQualifications.getIssuingUnit());
        existingCurrentQualifications.setRemarks(updatedCurrentQualifications.getRemarks());
        existingCurrentQualifications.setUpdatedAt(LocalDateTime.now());

        return currentQualificationsRepository.save(existingCurrentQualifications);
    }

    public void deleteCurrentQualifications(Long id) {
        currentQualificationsRepository.deleteById(id);
    }
}

